/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deadlocksim.backend.io;
import deadlocksim.backend.process.*;
import deadlocksim.backend.resources.*;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;



/**
 *
 * @author romilian
 */
public class ProcessFileLoader {
    
    public static final String REQUEST_RESOURCE = "R";
    public static final String FREE_RESOURCE = "F";
    public static final String COMPUTE = "C";
    public static final String HALT = "H";

    //public static String inFileName = "proc-exec-in.txt";
    public String inFileName;
    //"C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/DeadlockSim/proc-exec-in.txt";
    //public static String outFileName = "proc-exec-out.txt";
    
    //public static void main(String[] args) throws IOException {
    public ProcessFileLoader(String inFileName) {
        this.inFileName = inFileName;
    }
    
      public Queue<ProcessAction> BuildActionQueue() throws IOException {
        BufferedReader inputStream = null;
        PrintWriter outputStream = null;
        Queue<ProcessAction> pActions = new LinkedList<ProcessAction>();
        
        try {
            inputStream = 
                new BufferedReader(new FileReader(inFileName));
            //outputStream = 
            //    new PrintWriter(new FileWriter(outFileName));
                
            
            String l;
            while ((l = inputStream.readLine()) != null) {
                
                String[] words = l.trim().toUpperCase().split("\\s+"); // Separated by "whitespace"
                
                if(words.length == 0)
                {
                    emptyLineError();
                    return null;
                }
                //for(i=0; l.charAt(i)==' '; i++);  //skip any leading blanks
                if(words[0].equals(REQUEST_RESOURCE))
                {
                    if(words.length < 2 || words[1] == null)
                    {
                        resourceFormatError();
                        return null;
                    }
                    String resourceIdString = words[1];
                    
                    Integer resourceID;
                    Integer resourceCount;
                    try
                    {
                        resourceID = new Integer(resourceIdString);
                        pActions.add(new RequestResourceAction(resourceID.intValue()));                        
                    }
                    catch(Exception e)
                    {
                        resourceFormatError();
                    }
                }
                
                else if(words[0].equals(FREE_RESOURCE))
                {
                    if(words.length < 2 || words[1] == null)
                    {
                        resourceFormatError();
                        return null;
                    }
                    String resourceIdString = words[1];
                    
                    Integer resourceID;
                    try
                    {
                        resourceID = new Integer(resourceIdString);
                        pActions.add(new FreeResourceAction(resourceID.intValue()));
                    }
                    catch(Exception e)
                    {
                        resourceFormatError();
                    }
                }
                
                else if(words[0].equals(COMPUTE))
                {
                    if(words.length < 2 || words[1] == null)
                    {
                        computeFormatError();
                        return null;
                    }
                    String computeTimeString = words[1];
                    
                    Integer computeTime;
                    try
                    {
                        computeTime = new Integer(computeTimeString);
                        for(int i = 0; i < computeTime; i++)
                            pActions.add(new ComputeAction());
                    }
                    catch(Exception e)
                    {
                        computeFormatError();
                    }
                }
                
                else if(words[0].equals(HALT))
                {
                    pActions.add(new HaltAction());
                }
                
                else
                {
                    unrecognizedActionError();
                }
                
//                switch(words[0])   {
//                    case "R":
//                        for (j=i+1; l.charAt(j)==' '; j++); //skip any blanks after 'R'
//                        rsrcTypeId = l.charAt(j) - '0';
//                        //RequestResourceAction actionR = new RequestResourceAction(rsrcTypeId);
//                        //testActionStack.add(actionR);
//                        outputStream.println("encountered request for " + rsrcTypeId);
//                        break;
//                    case 'F':
//                        for (j=i+1; l.charAt(j)==' '; j++); //skip any blanks after 'F'
//                        rsrcTypeId = l.charAt(j) - '0';                        
//                        outputStream.println("encountered free for " + rsrcTypeId);
//                        break;
//                    case 'C':
//                        for (j=i+1; l.charAt(j)==' '; j++); //skip any blanks after 'C'
//                        rsrcTypeId = l.charAt(j) - '0';
//                        outputStream.println("encountered compute for ");
//                        break;
//                    case 'H':
//                        break;
//                    default:
//                        break;
            }
                
                //outputStream.println(l);
            
        } catch(Exception e)
        {
            System.err.println("Could not load file " + inFileName);
        } finally 
        {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
            return pActions;
        }
    }

    private static void computeFormatError() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private static void emptyLineError() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private static void resourceFormatError() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void unrecognizedActionError() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    

    
    
    
    
}
